﻿using System;
using System.Configuration;
using System.IO;
using System.Web;

namespace music.domain.Services
{
	public static class FileService
	{
		public static string FilesVirtualPath
		{
			get
			{
				var vpath = ConfigurationManager.AppSettings["files"];

				if (string.IsNullOrWhiteSpace(vpath))
				{
					vpath = "~/Content/files";
				}

				return vpath.Trim();
			}
		}

		public static string GetFilePath(Guid fileId)
		{
			string dir = HttpContext.Current.Server.MapPath(FilesVirtualPath);
			string fileName = string.Format("{0}.mp3", fileId);
			string path = Path.Combine(dir, fileName);

			return path;
		}

		public static void SaveFile(Guid fileId, byte[] bytes)
		{
			string path = GetFilePath(fileId);
			File.WriteAllBytes(path, bytes);
		}

		public static byte[] GetFile(Guid fileId)
		{
			string path = GetFilePath(fileId);
			return File.ReadAllBytes(path);
		}
	}
}
